package teaching.service;

import org.springframework.stereotype.Service;
import teaching.mapper.ChapterMapper;
import teaching.pojo.Chapter;
import teaching.pojo.ChapterRequest;
import teaching.pojo.Point;
import teaching.pojo.Questions;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author:20201002983_郑雨蓓
 * Date: 2024/3/12
 * Description:
 */
@Service
public class ChapterServise {
    @Resource
    private ChapterMapper chapterMapper;

    public int create(Chapter chapter) {
        return chapterMapper.add(chapter);
    }

//    public int create(List<Chapter> chapterList) {
//        int num = 0;
//        for (Chapter chapter : chapterList) {
//            num += chapterMapper.add(chapter);
//        }
//        return num;
//    }


    public int delete(Long id) {
        int num = chapterMapper.deleteChapter(id);
        chapterMapper.resetAutoIncrement();
        return num;
    }

    public List<Chapter> listByCourse(Long cid) {
        return chapterMapper.getChapterbyCid(cid);
    }


    public int update(Chapter newChapter) {
        return chapterMapper.updateChapter(newChapter);
    }

    public int addChapter(ChapterRequest request) {
        chapterMapper.insertChapter(request.getChapter());
        long chapterId = request.getChapter().getId();
        System.out.println("chapterId");
        if (request.getPointIds() != null && request.getPointIds().length > 0) {
            chapterMapper.insertChapterPoints(chapterId, request.getPointIds());
        }
        // 插入章节关联的习题
        if (request.getQuestionIds() != null && request.getQuestionIds().length > 0) {
            chapterMapper.insertChapterQuestions(chapterId, request.getQuestionIds());
        }
        return 1;
    }


    public List<Questions> findQuestionsByChapterId(long chapterId) {
        return chapterMapper.findQuestionsByChapterId(chapterId);
    }

    public List<Point> findPointByChapterId(long chapterId) {
        return chapterMapper.findPointsByChapterId(chapterId);
    }

    public Chapter findChapterById(Long id) {
        return chapterMapper.getChapterbyId(id);
    }
}
